﻿using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Util;
using AnesSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls
{
    /// <summary>
    /// XuanZeShouShuCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class XuanZeShouShuCtrl : UserControl
    {
        #region Variables

        private PatientLinkModel _selPatientLink;

        private List<RadioButton> rbos;


        #endregion

        #region Constructor

        public XuanZeShouShuCtrl()
        {
            InitializeComponent();
            rbos = new List<RadioButton>();
            this.DataContext = XuanZeShouShuCtrlViewModel.Instance;
            this.Loaded += XuanZeShouShuCtrl_Loaded;
            this._stateCmbCtrl.SelectedChanged += _stateCmbCtrl_SelectedChanged;
            _delete.Visibility = XuanZeShouShuCtrlViewModel.Instance.DeleteIsVis;
        }

        #endregion

        #region Properties

        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void FindAllRadioButton()
        {
            rbos = Common.UIHelper.FindVisualChild<RadioButton>(_xuanZeDataGrid);
        }

        #endregion

        #region Public/Protected Methods

        #endregion

        #region EventHandlers

        private void XuanZeShouShuCtrl_Loaded(object sender, RoutedEventArgs e)
        {
           XuanZeShouShuCtrlViewModel.Instance.RefView();
        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            try
            {
                string command = (sender as Button).Tag.ToString();
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;
                Task.Factory.StartNew(delegate
                {
                    Dispatcher.Invoke(delegate
                    {
                        XuanZeShouShuCtrlViewModel.Instance.Excute(command);
                        MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                    });
                });
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        //刷新按钮
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string command = (sender as Button).Tag.ToString();
            XuanZeShouShuCtrlViewModel.Instance.Excute(command);
        }

        //根据手术间获取手术
        private void _roomsCmbCtrl_SelectedChanged(object sender, SelectionChangedEventArgs e)

        {
            if (e.AddedItems.Count != 0)
            {
                string command = "qhssj";
                XuanZeShouShuCtrlViewModel.Instance.Excute(command);
            }
        }
        //根据手术状态获取手术
        private void _stateCmbCtrl_SelectedChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count != 0)
            {
                string command = "sszt";
                XuanZeShouShuCtrlViewModel.Instance.Excute(command);
            }
        }

        //DataGrid双击
        private void _xuanZeDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            XuanZeShouShuCtrlViewModel.Instance.Excute("dataGridMouseDouble");
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (DataManager.Instance.CurrentPatientLink == null)
            {
                MessageBoxCommon.ShowDialog("请选择一个术前手术再重排班！", MessageBoxCommon.OK);
                return;
            }
            if (DataManager.Instance.CurrentPatientLink.Application.Detailstatus.Equals("已排班"))
            {
                DataManager.Instance.CurrentPatientLink = PatientLinkService.Instance.GetObjById(DataManager.Instance.CurrentPatientLink.Id.ToString());
                RetypesettingCtrl retypesettingCtrl = new RetypesettingCtrl();
                DrugAndEventWindow.Instance.CusContent = retypesettingCtrl;
                DrugAndEventWindow.Instance.ShowDialog();
            }
            else
            {
                MessageBoxCommon.ShowDialog("只能重排班术前手术！", MessageBoxCommon.OK);
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            XuanZeShouShuCtrlViewModel.Instance.Export();
            MessageBoxCommon.ShowDialog("手术记录导出完成!", MessageBoxCommon.OK);
        }

        private void del_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            if (MessageBoxCommon.ShowDialog("确定要撤销此病人？", MessageBoxCommon.OKCANCLE).Value == true)
            {
                SelPatientLinkModel patientLinkModel = (sender as Button).DataContext as SelPatientLinkModel;
                XuanZeShouShuCtrlViewModel.Instance.UpdateStatusForRemove(patientLinkModel);
                VitalSignCapture.Instance.StopCapture();
                HospitalReturnUtil.Instance.ReturnOperStatus2His(DataManager.Instance.CurrentPatientLink);
                if (DataManager.Instance.CurrentPatientLink != null && patientLinkModel.PatientLink.Id.Equals(DataManager.Instance.CurrentPatientLink.Id))
                {
                    DataManager.Instance.CurrentPatientLink = null;
                }
            }
            else
            {
                return;
            }
        }

        #endregion

    }
}
